<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link href="../../../public/css/index-php-style.css" rel="stylesheet" />
    <link href="../../../public/css/cbl.css" rel="stylesheet" />
    <link href="../../../public/css/content.css" rel="stylesheet" />
    <style>
        body {
            background-color: #f2f3f5;
            padding-top: 6rem;
        }

        .navbar {
            background-color: #fff !important;
        }
    </style>
</head>

<body>
    <!-- 引入nav导航栏 -->
    <?php require '../../../nav.php'; ?>

    <?php
    $news = array();

    if (!empty($_GET)) {
        $nid = $_GET['nid'];

        // require '../../database/db.php';
        $link = db_connect();

        if (!$link) {
            die("数据库连接失败: " . mysqli_connect_error());
        }

        $sql = "SELECT * FROM news WHERE nid = ?;";

        // 创建预处理语句
        $stmt = mysqli_prepare($link, $sql);

        // 绑定参数
        mysqli_stmt_bind_param($stmt, "i", $nid);

        // 执行查询
        mysqli_stmt_execute($stmt);

        // 获取结果集
        $result = mysqli_stmt_get_result($stmt);

        // 处理结果集
        while ($rs = mysqli_fetch_assoc($result)) {
            $date = date("Y-m-d", strtotime($rs['addTime'] ? $rs['addTime'] : ''));

            $news['nid'] = !empty($rs['nid']) ? $rs['nid'] : null;
            $news['title'] = !empty($rs['title']) ? $rs['title'] : null;
            $news['content'] = !empty($rs['content']) ? $rs['content'] : null;
            $news['userid'] = !empty($rs['userid']) ? $rs['userid'] : null;
            $news['addTime'] = !empty($date) ? $date : null;
            $news['views'] = !empty($rs['views']) ? $rs['views'] : 0;
            $news['likes'] = !empty($rs['likes']) ? $rs['likes'] : 0;
            $news['comments'] = !empty($rs['comments']) ? $rs['comments'] : 0;
            $news['username'] = null;

            if (!empty($rs['userid'])) {
                $sql2 = "SELECT username FROM user where uid='{$rs['userid']}';";
                $res = mysqli_query($link, $sql2);
                while ($rs2 = mysqli_fetch_object($res)) {
                    $news['username'] = !empty($rs2->username) ? $rs2->username : null;
                }
            }
        }

        // 关闭预处理语句和数据库连接
        mysqli_stmt_close($stmt);
        mysqli_close($link);
    }
    ?>


    <div class="container con-box">
        <div class="row">
            <div class="col-sm-9 col-md-9">

                <?php if (!empty($news)) : ?>
                    <div class="content-bg">
                        <h2><?php echo $news['title'] ?></h2>

                        <div class="user-box">
                            <div class="user-name">
                                <span><?php echo $news['username'] ?></span>
                            </div>
                            <div class="user-tv">
                                <span class="user-time"><?php echo $news['addTime'] ?></span>
                                <span class="user-views">阅读 <?php echo $news['views'] ?></span>
                            </div>
                        </div>

                        <p class="content-p">
                            <?php echo $news['content'] ?>
                        </p>
                    </div>
                <?php endif; ?>
            </div>

            <div class="col-sm-3 col-md-3">
                <?php require '../../../cbl.php'; ?>
            </div>
        </div>
    </div>


    <script src="../../../public/js/jquery-3.7.0.min.js"></script>
    <script src="../../../public/js/bootstrap.bundle.min.js"></script>
    <script src="../../../public/js/getApiData.js"></script>
    <script src="../../../public/js/changeContent.js"></script>
</body>

</html>